/***************************************************************************
 *   Copyright (C) 2004 by Nilesh Bansal                                   *
 *   nilesh@iitb.ac.in                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef FTP_MONITOR_LOOP_H
#define FTP_MONITOR_LOOP_H

#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QVector>

/**
 * FTPMonitorLoop encapsulate start of "who" command, create "who" command and it's arguaments, it doesn't know
 * about KConfigGroup nothing, all settings save/load are doing through the FTPMonitor class
 * Command mFtpWhoCommand which is execute each n seconds, is generated before using createExecCommand QTimer starts
 */
class FTPMonitorLoop : public QObject
{
Q_OBJECT
public:
	struct FtpWhoCommand{
		QString bin;
		QStringList args;
	};

	enum FtpServerType {
		proftpd=1,pureftpd=2,vsftpd=3,ncftpd=4
	};

	FTPMonitorLoop();
	~FTPMonitorLoop();

	void start(int interval,bool allowPopup);
	void stop();

	virtual void createExecCommand(bool useSudo,QString whoPath)=0;
	virtual FtpServerType ftpServerType()=0;
private slots:
	/**
	 * this slot is called each n seconds
	 */
	void execWhoCommand();
	/**
	 * is called after command who/ps exec and call next parse method depended on actual ftp server
	 */
	void readFromStdout(int exitCode,QProcess::ExitStatus exitStatus);
	/**
	 * catch error signal from qprocess
	 */
	void execError ( QProcess::ProcessError error );
	/**
	 * process stdout from ftpdaemon
	 */
	virtual	void processStdOut(QString stdout)=0;
	/**
	 *
	 */
	virtual QString ftpdServerName()=0;
private:
	/**
	 * check if result of diff between two rows are meant as client state change
	 * @param newClientRow newer list of clients
	 * @param oldClientRow older list of clients
	 * @return.true if old and new is different false if are the same
	 */
	virtual bool ftpClientDiff(QStringList newClientRow,QStringList oldClientRow)=0;
	/**
	 * using pidof check if ftpd server is running
	 */
	bool ftpdServerIsRunning(QString);
	/**
	 * use "which" command to check if ftpwho command is available
	 */
	bool whichFtpwho(QString ftpdServerName);
	/**
	 * return executed ftpwho binary with or without path
	 */
	QString getExecutedFtpWho();
	/**
	 * vector of clients row which are currently displayed (each row contain e.g pid,username,hostname)
	 */
	QVector<QStringList> mListOfClients;
	/**
	 * command which is executed each n seconds using qtimer, this command could be set only
	 * trought the createExecCommand before qtimer start
	 */	
	bool mUseSudo;
	/**
	* tipe of ftpdaemon which is currently active
	*/
	FtpServerType mFtpDaemon;
	/**
	* hold info if ftpmonitor should popup info windows about new clients
	*/
	bool mAllowPopup;
	/**
	* is used for vsftpd, as it needs output redirection to grep, in this case mProcBeforeGrep execute
	* who/ps command and mProc exec grep command
	*/
	QProcess *mProcBeforeGrep;
	/**
	 * 
	 */
	int mLastQProcessError;
	/**
	 *
	 */
	int mLastFtpWhoExitValue;
	/**
	 * 
	 */
	QTimer *mTimer;
protected:
	/**
	 * this method check differences between older list of clients and newer list of clietns
	 * it call signals to add/del/change listView in FtpMonitor class
	 * @param QVector<QStringList> list of clients
	 */
	void searchForClientsStateChange(QVector<QStringList> listOfClients);
	/**
	* hold info if ftpmonitor should popup info windows about new clients
	*/
	FtpWhoCommand mFtpWhoCommand;
	/**
	 * command which is executed each n seconds using qtimer, this command could be set only
	 * trought the createExecCommand before qtimer start
	 */	
	QProcess *mProc;
signals:
	void sigAddRow(QStringList row);
	void sigDelRow(int row);
	void sigChangeRow(QStringList,int);
	//in case when we detect that count of client has changed we could update icon with proper number 
	void sigClientsCountChanged(int);
	//we can't bind sigClientsCountChanged to information about new clients e.g. 2 clients are new 2 were gone
	//ClientsCount was not changed but we have 2 new clients
	void sigNewClientsConnected(int count);
	//emit signal that something bad happen (we could change tray icon using this info)
	void sigErrorOccurs(int,QString);
};
#endif
